package com.example.demo.controller;


import com.example.demo.pojo.Question;
import com.example.demo.service.Impl.QuestionServiceImpl;
import com.example.demo.util.Unite;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class QuestionController {

    @Autowired
    QuestionServiceImpl questionService;
    @Autowired
    HttpServletRequest request;



    @GetMapping("/getQuestions")
    public Unite getQuestions(){
        List<Question> list=questionService.getAllQuestion();
        return new Unite(list,true);
    }
    @GetMapping("/getQuestion")
    public  Unite getQuestion(@RequestParam int question_id){
        Question question=questionService.getById(question_id);
        return new Unite(question,true);
    }

    @GetMapping("/getQuestionsById")
    public Unite getQuestionsById(@RequestParam int user_id){
        List<Question> list=questionService.getQuestionById(user_id);
        return new Unite(list,true);
    }
    @GetMapping("insertQuestion")
    public Unite insertQuestion(@RequestParam String question_summary,@RequestParam String question_details){
        int user_id=(Integer) request.getSession().getAttribute("user");
        Question question=new Question();
        question.setQuestion_summary(question_summary);
        question.setQuestion_details(question_details);
        questionService.insertQuestion(question,user_id);
        return new Unite(questionService.getAllQuestion(),true);
    }
    @GetMapping("deleteQuestion")
    public Unite deleteQuestion(@RequestParam int id){
        questionService.removeById(id);
        return null;
    }
}
